package com.newtouch.avmarketing.mgr.modules.avmarkrting.service.impl;

import com.newtouch.avmarketing.mgr.modules.avmarkrting.entity.DoubleAuditing;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.model.DoubleAuditingCondition;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.repo.DoubleAuditingRepo;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.service.DoubleAuditingService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DoubleAuditingServiceImpl implements DoubleAuditingService {

    @Autowired
    private DoubleAuditingRepo doubleAuditingRepo;

    @Override
    public Page<DoubleAuditing> findDoubleAuditing(DoubleAuditingCondition doubleAuditingCondition) {
        /**
         * 用户封装查询条件
         */
        Specification<DoubleAuditing> spec = new Specification<DoubleAuditing>() {
            /**
             * Predicate 封装单个查询条件
             * @param root 查询对象的属性封装
             * @param query 封装了要执行查询中的各个部分的信息  select from order by
             * @param cb 定义不同查询条件
             * @return
             */
            @SneakyThrows
            @Override
            public Predicate toPredicate(Root<DoubleAuditing> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<>();
                //投保单号
                if (!StringUtils.isEmpty(doubleAuditingCondition.getPolicyCode())) {
                    predicateList.add(cb.like(root.get("policyCode"), "%" + doubleAuditingCondition.getPolicyCode() + "%"));
                }
                //营销员工号
                if (!StringUtils.isEmpty(doubleAuditingCondition.getMarketEmp())) {
                    predicateList.add(cb.like(root.get("marketEmp"), "%" + doubleAuditingCondition.getMarketEmp() + "%"));
                }
                //审核员工号
                if (!StringUtils.isEmpty(doubleAuditingCondition.getCheckEmp())) {
                    predicateList.add(cb.like(root.get("checkEmp"), "%" + doubleAuditingCondition.getCheckEmp() + "%"));
                }
                //审核员工号
                if (!StringUtils.isEmpty(doubleAuditingCondition.getCheckStatus())) {
                    predicateList.add(cb.equal(root.get("checkStatus"), doubleAuditingCondition.getCheckStatus()));
                }
                //起始日期
                if (!StringUtils.isEmpty(doubleAuditingCondition.getBegin()) && !doubleAuditingCondition.getBegin().trim().equals("")) {
                    predicateList.add(cb.greaterThanOrEqualTo(root.get("insureTime"),doubleAuditingCondition.getBegin()));
                }
                //结束日期
                if (!StringUtils.isEmpty(doubleAuditingCondition.getEnd()) && !doubleAuditingCondition.getEnd().trim().equals("")) {
                    predicateList.add(cb.lessThanOrEqualTo(root.get("insureTime"), doubleAuditingCondition.getEnd()));
                }
                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        //排序
        Sort sort = new Sort(Sort.Direction.DESC, "insureTime");
        Pageable pageable = PageRequest.of(doubleAuditingCondition.getPageNumber()-1,doubleAuditingCondition.getPageSize(),sort);
        Page<DoubleAuditing> list = doubleAuditingRepo.findAll(spec, pageable);
        return list;
    }

    @Override
    public DoubleAuditing findByPolicyCode(String policyCode) {
        return doubleAuditingRepo.findByPolicyCode(policyCode);
    }

    @Override
    public void updateDoubleAuditing(String policyCode) {
        DoubleAuditing byPolicyCode = doubleAuditingRepo.findByPolicyCode(policyCode);
        byPolicyCode.setCheckStatus("2");
        doubleAuditingRepo.save(byPolicyCode);
    }

    @Override
    public void updateDoubleAuditingPass(String policyCode) {
        DoubleAuditing byPolicyCode = doubleAuditingRepo.findByPolicyCode(policyCode);
        byPolicyCode.setCheckStatus("1");
        doubleAuditingRepo.save(byPolicyCode);
    }

    @Override
    public void updateDoubleAuditingEndTime(String policyCode,String endTime) {
        DoubleAuditing byPolicyCode = doubleAuditingRepo.findByPolicyCode(policyCode);
        byPolicyCode.setEndTime(endTime);
        doubleAuditingRepo.save(byPolicyCode);
    }
}
